package yxy.game.pm2.active.command.monthcard;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_月卡;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActMonthCardDrawVipRewardRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;

/**
 * 领取月卡vip每日奖励
 * 
 * @author Dell
 *
 */
@MessageType(proto = Command.ActMonthCardDrawVipReward, classtype = ActiveMessage.ActMonthCardDrawVipRewardRequest.class)
public class CMD_Active_月卡_DrawVipReward extends Command<ActMonthCardDrawVipRewardRequest> {

	@Override
	public void execute(MessageSource messageSource, ActMonthCardDrawVipRewardRequest request) throws Exception {
		String pcode = messageSource.getTag();
		ActiveMessage.ActMonthCardDrawVipRewardResponse.Builder response = ActiveMessage.ActMonthCardDrawVipRewardResponse.newBuilder();

		ActiveModule_月卡 am = ActiveModule_月卡.get(pcode,false);
		int state = am.cangetvipmcreward();
		if (state == DrawStatus.cannotget.getStatus()) {
			send(messageSource, response, Result.ActCannotGet);
			return;
		}
		if (state == DrawStatus.hadget.getStatus()) {
			send(messageSource, response, Result.ActHadGet);
			return;
		}

		ArrayList<Currency> curs = am.handlevipreward();
		am.updateviprewardtime();
		CurrencyHandle ch = new CurrencyHandle();
		ch.addCurrency(pcode, CCReason.活动_月卡, curs);

		for (int i = 0; i < curs.size(); i++) {
			PackageMessage.Item.Builder item = PackageMessage.Item.newBuilder();

			Currency c = curs.get(i);
			item.setType(c.getType().getNumber());
			item.setDataId(c.getDataId());
			item.setNumber(c.getNumber());
			response.addDisplay(item.build());
			response.addUpdate(item.build());
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActMonthCardDrawVipRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
